package matrix;

/**
 * Абстрактный итератор по {@linkplain matrix.ICell Ячейкам} {@linkplain matrix.IRange Области}.
 * На откуп разработчикам отдаётся метод {@linkplain java.util.Iterator#remove() remove()}
 */
public class CSimpleRangeIterator implements IRangeIterator {
	private int m_iCurrentRow = 1;
	private int m_iCurrentColumn = 1;
	private IRange m_oRange;
	
	public CSimpleRangeIterator(IRange aRange) {
		this.m_oRange = aRange;
	}

	protected IRange getRange() {
		return this.m_oRange;
	}

	@Override
	public final boolean hasNext() {
		return ((m_iCurrentRow <= m_oRange.rows()) && (m_iCurrentColumn < m_oRange.columns()));
	}

	@Override
	public final ICell next() {
		ICell cell = m_oRange.cell(m_iCurrentRow, m_iCurrentColumn);
		if (m_iCurrentColumn == m_oRange.columns() && (m_iCurrentRow < m_oRange.rows())) {
			m_iCurrentColumn = 1;
			++m_iCurrentRow;
		} else {
			++m_iCurrentColumn;
		}
		return cell;
	}

	@Override
	public final void remove() {
		m_oRange.remove(m_oRange.cell(m_iCurrentRow, m_iCurrentColumn));
	}

	@Override
	public final IRange nextRow() {
		IRange range = m_oRange.row(m_iCurrentRow);
		++m_iCurrentRow;
		return range;
	}

	@Override
	public final IRange nextColumn() {
		IRange range = m_oRange.row(m_iCurrentColumn);
		++m_iCurrentColumn;
		return range;
	}

	@Override
	public final boolean hasNextRow() {
		return (m_iCurrentRow < m_oRange.rows());
	}

	@Override
	public final boolean hasNextColumn() {
		return (m_iCurrentColumn < m_oRange.columns());
	}

}
